/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.PerusahaanDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.PerusahaanDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Murid;
import com.ivanbiz.model.Perusahaan;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Iframe;

/**
 *
 * @author IW20149X
 */
public class LaporanKelulusanViewService extends SelectorComposer<Component> {

    @Wire
    Iframe ipdf;
    DaftarKelasDAO daftarKelasDAO;
    PerusahaanDAO perusahaanDAO;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        perusahaanDAO = new PerusahaanDAOImpl();
        List<Perusahaan> listPerusahaan = perusahaanDAO.getAll(Perusahaan.class);
        Perusahaan perusahaan = listPerusahaan.get(0);
        perusahaan.setAlamat(perusahaan.getAlamat() + "\n" + "Ph  :" + perusahaan.getTelephone() + "\n" + "Fax :" + perusahaan.getFax());

        daftarKelasDAO = new DaftarKelasDAOImpl();
        Kelas kelas = (Kelas) Executions.getCurrent().getArg().get("kelas");
        List<DaftarKelas> listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(kelas.getTransactionReference());

        Map map = new HashMap();
        map.put("perusahaan.alamat", perusahaan.getAlamat());
        map.put("kelas.alamatKelas", kelas.getAlamatKelas() + "\n" + "bertempat di : " + kelas.getTempatKelas());
        map.put("peserta", listDaftarKelas.size());
        map.put("logo", Executions.getCurrent().getDesktop().getWebApp().getRealPath("/img/logo.jpg"));

        String reportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/DaftarKelasKelulusanReport.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, map, new JRBeanCollectionDataSource(listDaftarKelas));
        loadToStream(jasperPrint);
    }

    private void loadToStream(JasperPrint jasperPrint) throws JRException, IOException {
        ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bytesOutputStream);

        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bufferedOutputStream);
        exporter.exportReport();

        InputStream mediais = new ByteArrayInputStream(bytesOutputStream.toByteArray());
        bytesOutputStream.close();

        AMedia amedia = new AMedia(jasperPrint.getName() + ".pdf", "pdf", "application/pdf", mediais);
        ipdf.setContent(amedia);
    }
}
